﻿// Type: GalaSoft.MvvmLight.ViewModelBase
// Assembly: GalaSoft.MvvmLight.SL5, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c114982fcf1a3a2e
// Assembly location: D:\DLL\GalaSoft.MvvmLight.SL5.dll

using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;

namespace GalaSoft.MvvmLight
{
  public abstract class ViewModelBase : ObservableObject, ICleanup
  {
    private static bool? _isInDesignMode;
    private IMessenger _messengerInstance;

    public bool IsInDesignMode
    {
      get
      {
        return ViewModelBase.IsInDesignModeStatic;
      }
    }

    public static bool IsInDesignModeStatic
    {
      get
      {
        if (!ViewModelBase._isInDesignMode.HasValue)
          ViewModelBase._isInDesignMode = new bool?(DesignerProperties.IsInDesignTool);
        return ViewModelBase._isInDesignMode.Value;
      }
    }

    protected IMessenger MessengerInstance
    {
      get
      {
        return this._messengerInstance ?? (IMessenger) Messenger.Default;
      }
      set
      {
        this._messengerInstance = value;
      }
    }

    public ViewModelBase()
      : this((IMessenger) null)
    {
    }

    public ViewModelBase(IMessenger messenger)
    {
      this.MessengerInstance = messenger;
    }

    public virtual void Cleanup()
    {
      Messenger.Default.Unregister((object) this);
    }

    protected virtual void Broadcast<T>(T oldValue, T newValue, string propertyName)
    {
      this.MessengerInstance.Send<PropertyChangedMessage<T>>(new PropertyChangedMessage<T>((object) this, oldValue, newValue, propertyName));
    }

    protected virtual void RaisePropertyChanged<T>(string propertyName, T oldValue, T newValue, bool broadcast)
    {
      base.RaisePropertyChanged(propertyName);
      if (!broadcast)
        return;
      this.Broadcast<T>(oldValue, newValue, propertyName);
    }

    protected virtual void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression, T oldValue, T newValue, bool broadcast)
    {
      if (propertyExpression == null)
        return;
      PropertyChangedEventHandler propertyChangedHandler = this.PropertyChangedHandler;
      if (propertyChangedHandler == null && !broadcast)
        return;
      MemberExpression memberExpression = propertyExpression.Body as MemberExpression;
      if (propertyChangedHandler != null)
        propertyChangedHandler((object) this, new PropertyChangedEventArgs(memberExpression.Member.Name));
      if (!broadcast)
        return;
      this.Broadcast<T>(oldValue, newValue, memberExpression.Member.Name);
    }

    protected void Set<T>(Expression<Func<T>> propertyExpression, ref T field, T newValue, bool broadcast)
    {
      if (EqualityComparer<T>.Default.Equals(field, newValue))
        return;
      T oldValue = field;
      field = newValue;
      this.RaisePropertyChanged<T>(propertyExpression, oldValue, field, broadcast);
    }

    protected void Set<T>(string propertyName, ref T field, T newValue, bool broadcast)
    {
      if (EqualityComparer<T>.Default.Equals(field, newValue))
        return;
      T oldValue = field;
      field = newValue;
      this.RaisePropertyChanged<T>(propertyName, oldValue, field, broadcast);
    }
  }
}
